package cn.jinjimi.app.main.activity;

import android.app.Dialog;
import android.content.Intent;
import android.view.View;
import android.widget.TextView;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import cn.jinjimi.app.AccountManager;
import cn.jinjimi.app.R;
import cn.jinjimi.app.ShareProxy;
import cn.jinjimi.app.bean.EventDataField;
import cn.jinjimi.app.bean.EventID;
import cn.jinjimi.app.controller.event.ShareCountEvent;
import cn.jinjimi.app.entity.Account;
import cn.jinjimi.app.entity.SharedCount;
import cn.jinjimi.app.main.dialog.ShareDialog;
import cn.jinjimi.app.main.extend.ToolBarCommonActivity;
import cn.jinjimi.base.util.AndroidUtils;
import cn.jinjimi.framework.helper.annotation.EventTrace;

/**
 * Created by haner on 14-12-30.
 */
public class InviteActivity extends ToolBarCommonActivity {

    private View shareFace;
    private View shareFirend;
    private Dialog shareDialog;
    private SharedCount sharedCount;
    private Account account;
    private String TAG = InviteActivity.class.getName();

    @Override
    protected void beforeInitView() {
        setContentView(R.layout.activity_share);
        setTitle(getString(R.string.me_invite_title));
    }

    @Override
    protected void initial() {
        initView();
        initListener();
        initData();
    }

    protected void initView() {
        shareFace = findViewById(R.id.invite_share_face);
        shareFirend = findViewById(R.id.invite_share_firend);
        TextView version = (TextView) findViewById(R.id.tv_version);
        version.setText(String.format(getString(R.string.version), AndroidUtils.getAppVersionName(this)));
    }

    protected void initListener() {
        shareFace.setOnClickListener(this);
        shareFirend.setOnClickListener(this);
    }

    protected void initData() {
        account = AccountManager.getInstance().getAccount();
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.invite_share_face:
                openInvitePage();

                break;
            case R.id.invite_share_firend:
                startDialog();
                break;
        }
    }

    @EventTrace(EventID.APP_OWNER_INVITE_FRIEND_SCAN)
    private void openInvitePage() {

    }

    @EventTrace(value = EventID.APP_SHARE_CLICK, paramsK = {EventDataField.ParamFrom}, paramsV = {EventID.FROM_INVITE_FRIEND})
    private void share(String url) {
        if (shareDialog == null)
            shareDialog = new ShareDialog.Builder()
                    .ShareUrl(url)
                    .FromUrl(EventID.FROM_INVITE_FRIEND)
                    .Content(getString(R.string.app_share_content))
                    .Title(String.format(getString(R.string.app_share_title), account.realName))
                    .ShareType(ShareProxy.ShareType.COMMON)
                    .build(this);
        shareDialog.show();
    }

    /**
     * 分享到平台
     */
    @EventTrace(EventID.APP_OWNER_INVITE_FRIEND_SHARE)
    private void startDialog() {
        share(sharedCount.shared_url);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onShareCountEvent(ShareCountEvent event) {
        if (isFinishing())
            return;
        share(sharedCount.shared_url);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }

}
